// ControlWindow.cpp - MARIN MSG Anneke Sicherer-Roetman 20081024

#include <stdlib.h> // exit
#include <math.h>   // atan
#include <stdarg.h>
#include <vector>
#include "defines.h"
#include "ControlWindow.h"
#include "assertplus.h"

/// pointer to control window (needed by callback functions)
static ControlWindow *pWindow = NULL;

// global callback functions in lin_wacu.cpp
/// @cond external
extern int getNumberOfWaypoints(int ship);
extern void toggleSimulation();
extern void toggleMovement();
extern void startLowering();
extern void togglePathVisible();
extern void toggleShipVisible();
extern void setZscale(float scale);
extern void saveWaves();

/// @endcond

// callback functions ----------------------------------------------------------
extern "C"
{

/// closes application    
void on_pathControlWindow_destroy(GtkObject *object, gpointer user_data)
{
    exit(0);
}

/// closes application
void on_quitButton_clicked(GtkObject *object, gpointer user_data)
{
    exit(0);
}

/// closes application
void on_saveButton_clicked(GtkObject *object, gpointer user_data)
{
    saveWaves();
}

/// changes z scale of plotter
void on_zscaleSlider_value_changed(GtkObject* object, gpointer)
{
    float zscale = gtk_range_get_value(GTK_RANGE(object));
    setZscale(zscale);
}

/// toggles simulation
void on_toggleSimulationButton_clicked(GtkObject *object, gpointer user_data)
{
    toggleSimulation();
}

/// toggles movement of ship
void on_toggleMovementButton_clicked(GtkObject *object, gpointer user_data)
{
    toggleMovement();
}

/// toggles simulation
void on_clickLowerButton_clicked(GtkObject *object, gpointer user_data)
{
    startLowering();
}

/// toggles visibility of waypoints
void on_togglePathButton_clicked(GtkObject *object, gpointer user_data)
{
    pWindow->setPathVisible(!pWindow->isPathVisible());
    togglePathVisible();
}

/// toggles visibility of ship
void on_toggleShipButton_clicked(GtkObject *object, gpointer user_data)
{
    pWindow->setShipVisible(!pWindow->isShipVisible());
    toggleShipVisible();
}

} // extern "C"
// -----------------------------------------------------------------------------

/// constructor
ControlWindow::ControlWindow(int *argc,              ///< from main
                             char ***argv,           ///< from main
                             int nrPulses)           ///< number of pressure pulses
    : GtkAppWindow(argc, argv, "controlWindow.xml", "compwaveControlWindow")
    , m_pathVisible(true)
    , m_shipVisible(true)
{
    // initialize
    ASSERT(!pWindow, "only one ControlWindow may be used because of callback mechanism");
    pWindow = this;
}
   

/// destructor
ControlWindow::~ControlWindow()
{
    pWindow = 0;
}


/// toggles visibility of waypoints
void ControlWindow::setPathVisible(bool on)
{
    ASSERTPOINTER(pWindow);
    m_pathVisible = on;
}
    

/// are waypoints visible?
bool ControlWindow::isPathVisible() const
{
    ASSERTPOINTER(pWindow);
    return m_pathVisible;
}

/// toggles visibility of ships
void ControlWindow::setShipVisible(bool on)
{
    ASSERTPOINTER(pWindow);
    m_shipVisible = on;
}


/// are ships visible?
bool ControlWindow::isShipVisible() const
{
    ASSERTPOINTER(pWindow);
    return m_shipVisible;
}
